package org.zhou.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.zhou.constant.MessageConstant;
import org.zhou.constant.StatusConstant;
import org.zhou.exception.PasswordErrorException;
import org.zhou.mapper.AdminMapper;
import org.zhou.pojo.Administrator;
import org.zhou.pojo.Book;
import org.zhou.pojo.PageResult;
import org.zhou.pojo.Readers;
import org.zhou.service.AdminService;
import org.zhou.exception.AccountNotFoundException;
import org.zhou.exception.AccountLockedException;
import org.zhou.utils.ThreadLocalUtil;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     *
     * @param username
     * @return
     */
    public Administrator login(String username) {

        //1、根据用户名查询数据库中的数据
        Administrator admin = adminMapper.getByUsername(username);
        log.info("根据用户名查询数据库中的数据,{}",admin);
        //3、返回实体对象
        return admin;
    }

    @Override
    public void updateInfo(Administrator admin) {
        adminMapper.updateInfo(admin);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
//        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
        adminMapper.updatePwd(newPwd,id);
    }

    @Override
    public PageResult readerList(Integer pageNum, Integer pageSize, String name, Integer state, String phone, String readerId) {
        //1.创建PageBean对象
        PageResult pb = new PageResult();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3.调用mapper
        List<Readers> as = adminMapper.readerList(name,state,phone,readerId);
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        Page<Readers> p = (Page<Readers>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public void addReader(Readers readers) {
        adminMapper.addReader(readers);
    }

    @Override
    public void updateReader(Readers readers) {
        adminMapper.updateReader(readers);
    }

    @Override
    public void addAdmin(String username, String password) {

        adminMapper.addAdmin(username,password);
    }

    @Override
    public void updateStatus(Integer status,String username) {
        adminMapper.updateStatus(status,username);
    }
}
